/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.favMenu.controller;

import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.favMenu.service.FavMenuService;
import com.jeeplus.sys.favMenu.service.dto.FavMenuDTO;
import com.jeeplus.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏菜单Controller
 *
 * @author jeeplus
 * @version 2023-09-05
 */
@RestController
@RequestMapping("/sys/favMenu")
public class FavMenuController {

    @Autowired
    private FavMenuService favMenuService;

    /**
     * 获取当前用户的收藏菜单列表
     *
     * @return 收藏菜单列表
     */
    @ApiLog("获取收藏菜单列表")
    @GetMapping("getList")
    public ResponseEntity<List<FavMenuDTO>> getFavMenuList() {
        String userId = UserUtils.getCurrentUserDTO().getId();
        List<FavMenuDTO> favMenuList = favMenuService.findByUserId(userId);
        return ResponseEntity.ok(favMenuList);
    }

    /**
     * 收藏菜单
     *
     * @param menuId 菜单ID
     * @return 操作结果
     */
    @ApiLog("收藏菜单")
    @PostMapping("add")
    public ResponseEntity<String> addFavMenu(@RequestParam String menuId) {
        String userId = UserUtils.getCurrentUserDTO().getId();
        boolean result = favMenuService.addFavMenu(userId, menuId);
        if (result) {
            return ResponseEntity.ok("收藏成功");
        } else {
            return ResponseEntity.badRequest().body("收藏失败");
        }
    }

    /**
     * 取消收藏菜单
     *
     * @param menuId 菜单ID
     * @return 操作结果
     */
    @ApiLog("取消收藏菜单")
    @PostMapping("remove")
    public ResponseEntity<String> removeFavMenu(@RequestParam String menuId) {
        String userId = UserUtils.getCurrentUserDTO().getId();
        boolean result = favMenuService.removeFavMenu(userId, menuId);
        if (result) {
            return ResponseEntity.ok("取消收藏成功");
        } else {
            return ResponseEntity.badRequest().body("取消收藏失败");
        }
    }

    /**
     * 检查菜单是否已收藏
     *
     * @param menuId 菜单ID
     * @return 是否已收藏 1111122
     */
    @ApiLog("检查菜单是否已收藏")
    @GetMapping("checkFaved")
    public ResponseEntity<Boolean> checkFaved(@RequestParam String menuId) {
        String userId = UserUtils.getCurrentUserDTO().getId();
        boolean isFaved = favMenuService.isFaved(userId, menuId);
        return ResponseEntity.ok(isFaved);
    }

    /**
     * 获取当前用户ID111
     *
     * @return 当前用户ID
     */
    private String getCurrentUserId() {
        return UserUtils.getCurrentUserDTO().getId();
    }

    /**
     * 获取当前用户ID（公开接口）
     *
     * @return 当前用户ID
     */
    @ApiLog("获取当前用户ID")
    @GetMapping("getCurrentUserId")
    public ResponseEntity<String> getCurrentUserIdApi() {
        return ResponseEntity.ok(getCurrentUserId());
    }
}